package com.aote.model.block;

import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.aote.util.ThreadLocals;
import com.browsesoft.Entity;
import com.browsesoft.EntityManager;
import com.browsesoft.ExtendElement;
import com.browsesoft.Tools;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLFormComponent;
import com.browsesoft.htmlcomponent.HTMLTableModel;
import com.browsesoft.resource.BasicResource;
import com.browsesoft.user.User;

/**
 * 显示实体的制定属性块模版
 */
public class ResourcesBlockModel implements HTMLTableModel {

	/**
	 * 组件
	 */
	protected HTMLBasicComponent component = null;

	/**
	 * 配置的实体属性名
	 */
	private String attrName = "";

	/**
	 * 数据，List的每一项为参数值对象
	 */
	public List datas = new LinkedList();

	public void init(HttpServletRequest request) throws Exception {

	}

	public void setComponent(HTMLBasicComponent component) {
		this.component = component;
	}

	public int getColumnCount() {
		return this.datas.size();
	}

	public String getColumnName(int index) {
		Entity entity = (Entity) this.datas.get(index);
		String result = (String) entity.getAttributes().get(this.attrName);
		return result;
	}

	public int getRowCount() {
		return 0;
	}

	public void setCondition(String condition) {
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
		String content = this.getColumnName(row);
		// 替换显示内容
		ExtendElement.replaceElementContext(td, content);
		// 替换连接
		LinkedList nl = ExtendElement.findElements(td, "type", "link");
		if (nl.size() > 0) {
			Element link = (Element) nl.get(0);
			String value = link.getAttribute("onclick");
			Entity entity = (Entity) this.datas.get(row);
			value = Tools.replaceAttrs(value, entity.getAttributes());
			// 父属性替换
			if (value.indexOf("#") != -1) {
				value = repalceParentAttr(value, this.component.getParent());
			}
			link.setAttribute("onclick", value);
		}
	}

	private String repalceParentAttr(String value, HTMLBasicComponent component) {
		if (component.getParent() != null) {
			if (component.getParent() instanceof HTMLFormComponent) {
				Entity entity = (Entity) component.getParent().getModel();
				value = Tools.replaceAttrs(value, entity.getAttributes());
			}
			value = this.repalceParentAttr(value, component.getParent());
		}
		return value;
	}

	public String getType(int col) {
		return null;
	}

	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// 得到配置的实体类型，实体显示的属性名
		String resourceType = this.component.getElement().getAttribute(
				"resourcetype");
		this.datas = this.getResources(resourceType);
		this.attrName = this.component.getElement().getAttribute("showattr");
	}

	/**
	 * 得到实体
	 */
	private List getResources(String resourceType) {
		try {
			User lg = (User) ThreadLocals.loginUser.get();
			LinkedList result = ((BasicResource) lg.getParent())
					.getAllChildrenByType(resourceType);
			return result;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
